<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人部门" prop="department" label-width="100">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入申请人部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人姓名" prop="name" label-width="100">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['reimbursement:BusinessTripReimbursement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reimbursement:BusinessTripReimbursement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reimbursement:BusinessTripReimbursement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['reimbursement:BusinessTripReimbursement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="BusinessTripReimbursementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="申请人部门" align="center" prop="department" />
      <el-table-column label="申请人姓名" align="center" prop="name" />
      <el-table-column label="出差开始时间" align="center" prop="travelStartDate" />
      <el-table-column label="出差结束时间" align="center" prop="travelEndDate" />
      <el-table-column label="出差地点" align="center" prop="travelLocation" />
      <el-table-column label="出差事由" align="center" prop="travelPurpose" />
      <el-table-column label="车船费" align="center" prop="transportCost" />
      <el-table-column label="住宿费" align="center" prop="accommodationCost" />
      <el-table-column label="途中补助天数" align="center" prop="subsidyDays" />
      <el-table-column label="途中补助金额" align="center" prop="subsidyAmount" />
      <el-table-column label="住勤地点" align="center" prop="onDutyLocation" />
      <el-table-column label="住勤天数" align="center" prop="onDutyDays" />
      <el-table-column label="住勤金额" align="center" prop="onDutyAmount" />
      <el-table-column label="总金额(小写)" align="center" prop="totalAmount" />
      <el-table-column label="总金额(大写)" align="center" prop="totalAmountUpper" />
      <el-table-column label="同行人员" align="center" prop="travelMates" />
      <el-table-column label="关联的出差申请单id" align="center" prop="travelRequestId" />
      <el-table-column label="是否开车" align="center" prop="isDriving" />
      <el-table-column label="交通补助" align="center" prop="travelSubsidy" />
      <el-table-column label="伙食补助" align="center" prop="mealSubsidy" />
      <el-table-column label="证明人" align="center" prop="proofPerson" />
      <el-table-column label="报销票据路径" align="center" prop="receiptImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.receiptImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['reimbursement:BusinessTripReimbursement:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['reimbursement:BusinessTripReimbursement:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出差报销对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="BusinessTripReimbursementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人部门" prop="department">
          <el-input v-model="form.department" placeholder="请输入申请人部门" />
        </el-form-item>
        <el-form-item label="申请人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="出差开始时间" prop="travelStartDate">
          <el-input v-model="form.travelStartDate" placeholder="请输入出差开始时间" />
        </el-form-item>
        <el-form-item label="出差结束时间" prop="travelEndDate">
          <el-input v-model="form.travelEndDate" placeholder="请输入出差结束时间" />
        </el-form-item>
        <el-form-item label="出差地点" prop="travelLocation">
          <el-input v-model="form.travelLocation" placeholder="请输入出差地点" />
        </el-form-item>
        <el-form-item label="出差事由" prop="travelPurpose">
          <el-input v-model="form.travelPurpose" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="车船费" prop="transportCost">
          <el-input v-model="form.transportCost" placeholder="请输入车船费" />
        </el-form-item>
        <el-form-item label="住宿费" prop="accommodationCost">
          <el-input v-model="form.accommodationCost" placeholder="请输入住宿费" />
        </el-form-item>
        <el-form-item label="途中补助天数" prop="subsidyDays">
          <el-input v-model="form.subsidyDays" placeholder="请输入途中补助天数" />
        </el-form-item>
        <el-form-item label="途中补助金额" prop="subsidyAmount">
          <el-input v-model="form.subsidyAmount" placeholder="请输入途中补助金额" />
        </el-form-item>
        <el-form-item label="住勤地点" prop="onDutyLocation">
          <el-input v-model="form.onDutyLocation" placeholder="请输入住勤地点" />
        </el-form-item>
        <el-form-item label="住勤天数" prop="onDutyDays">
          <el-input v-model="form.onDutyDays" placeholder="请输入住勤天数" />
        </el-form-item>
        <el-form-item label="住勤金额" prop="onDutyAmount">
          <el-input v-model="form.onDutyAmount" placeholder="请输入住勤金额" />
        </el-form-item>
        <el-form-item label="总金额(小写)" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额(小写)" />
        </el-form-item>
        <el-form-item label="总金额(大写)" prop="totalAmountUpper">
          <el-input v-model="form.totalAmountUpper" placeholder="请输入总金额(大写)" />
        </el-form-item>
        <el-form-item label="同行人员" prop="travelMates">
          <el-input v-model="form.travelMates" placeholder="请输入同行人员" />
        </el-form-item>
        <el-form-item label="关联的出差申请单id" prop="travelRequestId">
          <el-input v-model="form.travelRequestId" placeholder="请输入关联的出差申请单id" />
        </el-form-item>
        <el-form-item label="是否开车" prop="isDriving">
          <el-input v-model="form.isDriving" placeholder="请输入是否开车" />
        </el-form-item>
        <el-form-item label="交通补助" prop="travelSubsidy">
          <el-input v-model="form.travelSubsidy" placeholder="请输入交通补助" />
        </el-form-item>
        <el-form-item label="伙食补助" prop="mealSubsidy">
          <el-input v-model="form.mealSubsidy" placeholder="请输入伙食补助" />
        </el-form-item>
        <el-form-item label="证明人" prop="proofPerson">
          <el-input v-model="form.proofPerson" placeholder="请输入证明人" />
        </el-form-item>
        <el-form-item label="报销票据路径" prop="receiptImage">
          <image-upload v-model="form.receiptImage"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BusinessTripReimbursement">
import { listBusinessTripReimbursement, getBusinessTripReimbursement, delBusinessTripReimbursement, addBusinessTripReimbursement, updateBusinessTripReimbursement } from "@/api/reimbursement/BusinessTripReimbursement";

const { proxy } = getCurrentInstance();

const BusinessTripReimbursementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    department: null,
    name: null,
    status: null,
  },
  rules: {
    userId: [
      { required: true, message: "用户id不能为空", trigger: "blur" }
    ],
    department: [
      { required: true, message: "申请人部门不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "申请人姓名不能为空", trigger: "blur" }
    ],
    travelStartDate: [
      { required: true, message: "出差开始时间不能为空", trigger: "blur" }
    ],
    travelEndDate: [
      { required: true, message: "出差结束时间不能为空", trigger: "blur" }
    ],
    travelLocation: [
      { required: true, message: "出差地点不能为空", trigger: "blur" }
    ],
    travelPurpose: [
      { required: true, message: "出差事由不能为空", trigger: "blur" }
    ],
    transportCost: [
      { required: true, message: "车船费不能为空", trigger: "blur" }
    ],
    accommodationCost: [
      { required: true, message: "住宿费不能为空", trigger: "blur" }
    ],
    totalAmount: [
      { required: true, message: "总金额(小写)不能为空", trigger: "blur" }
    ],
    isDriving: [
      { required: true, message: "是否开车不能为空", trigger: "blur" }
    ],
    travelSubsidy: [
      { required: true, message: "交通补助不能为空", trigger: "blur" }
    ],
    mealSubsidy: [
      { required: true, message: "伙食补助不能为空", trigger: "blur" }
    ],
    proofPerson: [
      { required: true, message: "证明人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询出差报销列表 */
function getList() {
  loading.value = true;
  listBusinessTripReimbursement(queryParams.value).then(response => {
    BusinessTripReimbursementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    department: null,
    name: null,
    travelStartDate: null,
    travelEndDate: null,
    travelLocation: null,
    travelPurpose: null,
    transportCost: null,
    accommodationCost: null,
    subsidyDays: null,
    subsidyAmount: null,
    onDutyLocation: null,
    onDutyDays: null,
    onDutyAmount: null,
    totalAmount: null,
    totalAmountUpper: null,
    travelMates: null,
    travelRequestId: null,
    isDriving: null,
    travelSubsidy: null,
    mealSubsidy: null,
    proofPerson: null,
    receiptImage: null,
    createTime: null,
    status: null,
    isdel: null
  };
  proxy.resetForm("BusinessTripReimbursementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加出差报销";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBusinessTripReimbursement(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改出差报销";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["BusinessTripReimbursementRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBusinessTripReimbursement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBusinessTripReimbursement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除出差报销编号为"' + _ids + '"的数据项？').then(function() {
    return delBusinessTripReimbursement(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('reimbursement/BusinessTripReimbursement/export', {
    ...queryParams.value
  }, `BusinessTripReimbursement_${new Date().getTime()}.xlsx`)
}

getList();
</script>
